<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;
use DB;
class LinkController extends Controller
{
    public function linkadd(){

    //友情链接的模板
   	return view('Admin.link.linkadd');
    }
    //获取添加的信息
    public function link(Request $request){

   		 $res=$request->only(['content','link']);
    	// $res= $request->all();


   		
   		//对上传图片的处理
   		if($request->hasFile('img')){

   			$name = rand(1,9999).time();

   			$suffix=$request->file('img')->getClientOriginalExtension();

   			$arr=array('jpg','png','jpeg');

   			 $new = $name.'.'.$suffix;

   			if(!in_array($suffix,$arr)){
   				return back();
   			}

   			$request->file('img')->move('./upload',$new);
   		}

   		
   		if(!empty($new)){
			$res['img'] = $new;
		}else{
			return back();
		}

   		// dd($res);

   		//把查询的数据添加进入数据库
   		$ress = DB::table('link')->insert($res);
   		if($ress){
   			return redirect('/admin/link/list');
   		}else{
   			return back();
   		}

    }

    public function linklist(){

    $res =DB::table('link')->paginate(10);

    // dd($res);

     return view('Admin.link.linklist',['link'=>$res]);


    }
    //友情链接的删除操作
    public function getDelete($id){

  		$res = DB::table('link')->where('id',$id)->delete();

  		if($res){
  			return redirect('/admin/link/list');
  		}else{
  			return redirect('/admin/link/list');
  		}
    }

    //友情链接的修改操作

    public function getUpdate($id){

    $res = DB::table('link')->where('id',$id)->first();

   	return view('Admin.link.linkupdate',['link'=>$res]);	
    }
    //友情链接的执行修改操作
    public function postDoupdate(Request $request){

    	$res=$request->all();
    	
    	$res=$request->only(['content','link']);
    	$id = $request->only(['id']);
    	
   		//对上传图片的处理w
   		
   		if($request->hasFile('img')){

   			$name = rand(1,9999).time();

   			$suffix=$request->file('img')->getClientOriginalExtension();

   			$arr=array('jpg','png','jpeg');

   			 $new = $name.'.'.$suffix;

   			if(!in_array($suffix,$arr)){
   				return back();
   			}

   			$request->file('img')->move('./upload',$new);
   		}
		if(!empty($new)){
			$res['img'] = $new;
		}
   		

   		//把查询的数据添加进入数据库
   		$ress = DB::table('link')->where('id',$id)->update($res);
   		if($ress){
   			return redirect('/admin/link/list');
   		}else{
   			return back();
   		}
   	
    }

}
